package org.groupg.practice.guiPractice.layoutDemo;

import javax.swing.*;
import java.awt.*;

public class GridBagLayoutExample {
    public static void main(String[] args) {
        // 创建一个 JFrame
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // 设置为 GridBagLayout
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // 创建一些按钮和面板
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JPanel panel = new JPanel();
        panel.add(new JButton("Big Button"));

        // 添加组件到 JFrame 并设置 GridBagConstraints
        gbc.gridx = 0;
        gbc.gridy = 0;
        frame.add(button1, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        frame.add(button2, gbc);

        gbc.gridx = 2; // 第三列开始
        gbc.gridy = 0; // 第一行
        gbc.gridwidth = 2; // 占用两列
        gbc.fill = GridBagConstraints.BOTH; // 填充整个区域
        frame.add(panel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1; // 重置为默认占用1列
        frame.add(new JButton("Button 3"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        frame.add(new JButton("Button 4"), gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        frame.add(new JButton("Button 5"), gbc);

        // 第三个按钮后面的两个按钮(4,5)会在下一行

        // 显示 JFrame
        frame.setVisible(true);
    }
}